package com.sf.osms.dep.processor.handler.convert;

import java.util.Date;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sf.osms.dep.domain.Agent;
import com.sf.osms.dep.domain.Router;
import com.sf.osms.dep.processor.exception.ExceptionConstants;
import com.sf.osms.dep.processor.exception.RouterException;
import com.sf.osms.dep.processor.model.EventModel;
import com.sf.osms.dep.processor.util.DateUtil;
import com.sf.osms.dep.processor.util.ExceptionUtils;
import com.sf.osms.dep.processor.util.ReflectUtils;
import com.sf.osms.dep.processor.util.RouterConstants;
/**
 * 公用信息的路由转化，比如先将节点信息做一个全量的映射
 * @author sfhq1405
 *
 */
public class CommonConverter implements Converter{
	
	private static Logger logger = LoggerFactory.getLogger(CommonConverter.class);
	
	@Override
	public void convert(Router router, Map<String, String> routerFieldAndStr,
			Agent agentInfo, EventModel eventModel, ConverterChain chain)
			throws RouterException {
		Set<String> routerFileds = routerFieldAndStr.keySet();
		for (String fieldStr : routerFileds) {
			
			String filedVal = routerFieldAndStr.get(fieldStr);
			boolean flag = convert(fieldStr,filedVal, router, eventModel, agentInfo);
			if(flag){
				continue;
			}
			ReflectUtils.setFiledValue(router,fieldStr,filedVal);
		}
		
		chain.convert(router, routerFieldAndStr, agentInfo, eventModel, chain);
		
	}
	
	/**
	 * 若有不同转化逻辑，进行覆盖
	 * @param fieldStr
	 * @param filedVal
	 * @param router
	 * @param eventModel
	 * @param agentInfo
	 * @return
	 * @throws RouterException
	 */
	protected boolean convert(String fieldStr, String filedVal,Router router,EventModel eventModel,Agent agentInfo) throws RouterException {
		//进行eventcode的转换 注意解析出来的数据的空格
		if(fieldStr.equalsIgnoreCase("eventCode")){
			try{
				if(StringUtils.isNotBlank(filedVal)){
					router.setAgentEventCode(filedVal);
				}
			}catch (Exception e) {
				logger.error("******代理：" + agentInfo.getName() +",处理文件：" + eventModel.getFile().getAbsolutePath()+
						"," + "eventCode（事件节点）字段转化出现异常******");
				router.addFailReasonList(ExceptionConstants.HANDLE_EVENT_CODE_ERROR_CODE);
				logger.error(ExceptionUtils.getExceptionStackString(e));
			}
			
			return true;
		}
		
		//事件原因节点的转化
		if(fieldStr.equalsIgnoreCase("reasonCode")){
			try {
				if(StringUtils.isNotBlank(filedVal)){
					router.setAgentReasonCode(filedVal);
				}
			} catch (Exception e) {
				logger.error("******代理：" + agentInfo.getName() +",处理文件：" + eventModel.getFile().getAbsolutePath()+
						"," + "reasonCode（失败原因节点节点）字段转化出现异常******");
				router.addFailReasonList(ExceptionConstants.HANDLE_RESON_CODE_ERROR_CODE);
				logger.error(ExceptionUtils.getExceptionStackString(e));
			}
			
			return true;
		}
		
		//把枪时间的转化
		if(fieldStr.equalsIgnoreCase("barTime")){
			try {
				Date bar = DateUtil.formatTime(filedVal.trim());
				router.setBarTime(bar);
			} catch (Exception e) {
				logger.error("******代理：" + agentInfo.getName() +",处理文件：" + eventModel.getFile().getAbsolutePath()+
						"," + "barTime（把枪时间）字段转化出现异常******");
				router.addFailReasonList(ExceptionConstants.HANDLE_BAR_TIME_ERROR_CODE);
				logger.error(ExceptionUtils.getExceptionStackString(e));
			}
			
			return true;
		}
		
		//把枪日期的转化
		if(fieldStr.equalsIgnoreCase("barDate")){
			try {
				Date bar = DateUtil.formatDate(filedVal.trim());
				router.setBarDate(bar);
			} catch (Exception e) {
				logger.error("******代理：" + agentInfo.getName() +",处理文件：" + eventModel.getFile().getAbsolutePath()+
						"," + "barDate（把枪日期）字段转化出现异常******");
				router.addFailReasonList(ExceptionConstants.HANDLE_BAR_DATE_ERROR_CODE);
				logger.error(ExceptionUtils.getExceptionStackString(e));
			}
			
			return true;
		}
		
		//进行代理传递过来的网点的存放
		if(fieldStr.equals("netNum")){
			
			if(StringUtils.isNoneBlank(filedVal)){
				router.setAgentNetNum(filedVal);
			}
			
			return true;
		}
		
		//进行特殊eventcode数据解析
		if(fieldStr.startsWith(RouterConstants.AGENT_EVENT_CODE_SOURCE_FIELD_PREFIX)){
			if(StringUtils.isBlank(filedVal)){
				filedVal = "";
			}
			router.setAdjustEventCodeInfo(fieldStr,filedVal);
			return true;
		}
		
		
		return false;
	}
}
